/*
 * Golazo MA4
 * Copyright (C) 2008 Golazo MASHUP AWARD 4th team.
 * http://code.google.com/p/golazo/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * serverload
 */
function serverload() {

  // dispatch 
  Golazo.dispatch(function(request, response, session) {

    // 私は誰か
    var me = Golazo.User.find(session.get('me'));
	if (!me)
	  return $('#signin')
	    .layout(/*me*/)
	    .metaRefresh('/signin.html?' + $.param({ url: request.uri }), 3);
	
    // メモIDは期待どおりか
    var actionId = request.data.actionId;
    if (!Golazo.Action.checkActionId(actionId))
      throw new Error('(actionId)');
		
    // メモを検索する
	var action = Golazo.Action.find(actionId);
	if (!action)
      return $('#notfound')
	    .layout(me)
        .metaRefresh('/me.html', 3);

    // メモの作成者は期待どおりか
    var you;
	if (!me.equals(action.creatorId)) {
      you = Golazo.User.find(action.creatorId);
	  if (!you)
	    throw new Error('(creatorId)');
	}

    var actionId = $('#form_actionId')
      .val(action.actionId);

    var note = $('#form_note')
      .val(action.note);

    var latitude = $('#form_latitude')
      .val(action.latitude);
      
	var longitude = $('#form_longitude')
      .val(action.longitude);
      
	var altitude = $('#form_altitude')
      .val(action.altitude);

    // メモの購読者を検索する
	var subscribers = action.findBySubscribed();

    // メモの購読者かどうか
	if (!Golazo.find(subscribers, function() {
      return me.equals(this);
    })) {
      return $('#notfound')
	    .layout(me)
        .metaRefresh('/me.html', 3);
    }

    subscribers.forEach(function(user) {
      Golazo.User.Subscriber.htmlify(user, true/*readonly*/)
        .appendTo('#subscribers')
		.show();
    });

	return $(you ? '#succeeded,#you' : '#succeeded,#me')
	  .layout(me,you)
	  .maps();
  });
};

/**
 * load
 */
function load(layout) {

  $(document.form).submit(function() {
    return true;
  });

  Golazo.User.bind($('div.user', '#you'));

  // #succeeded
  if (layout.is('#succeeded')) {
    
	var latitude = $('#form_latitude');
    var longitude = $('#form_longitude');
    var altitude = $('#form_altitude');

    var point = new GLatLng(latitude.val(), longitude.val());
	var zoom = parseInt(altitude.val(), 10);

	// GMap
    var map = new GMap2(document.getElementById('map'));
    map.addControl(new GSmallMapControl());
    map.setCenter(point, zoom);

    var marker = new GMarker(point);
    map.addOverlay(marker);

    // GStreetview
    var pano = new GStreetviewPanorama(document.getElementById('pano'));

    var client = new GStreetviewClient();
    client.getNearestPanoramaLatLng(point, function(latlng) {
	  if (latlng) {
		$('#pano').show();
	    pano.setLocationAndPOV(point);
	  }
	});

    //var overlay = new GStreetviewOverlay()
	//map.addOverlay(overlay);
  }
};
load.runat = 'client';
